package com.itheima.project.project;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.google.gson.Gson;
import com.itheima.project.*;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class FragmentSencondProject extends Fragment {

    private static final String ID = "id";
    private RecyclerView rvContent;
    private FragmentSencondRecyclerAdapter fragmentSencondRecyclerAdapter;
    private boolean isRefresh = true;
    private List<SencondRecyclerEntity.DataBean.DatasBean> datas;


    public static FragmentSencondProject Instance(int id){
        FragmentSencondProject fragmentSencondProject = new FragmentSencondProject();
        Bundle bundle = new Bundle();
        bundle.putInt(ID,id);
        fragmentSencondProject.setArguments(bundle);
        return fragmentSencondProject;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View inflate = inflater.inflate(R.layout.fragment_sencond_project, null, false);
        rvContent = inflate.findViewById(R.id.rv_content_fragment_sencond_project);
        return inflate;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        final List<SencondRecyclerEntity.DataBean.DatasBean> mEneityList = new ArrayList<>();
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        rvContent.setLayoutManager(linearLayoutManager);
        fragmentSencondRecyclerAdapter = new FragmentSencondRecyclerAdapter(R.layout.fragment_sencond_project_item,datas);
        rvContent.setAdapter(fragmentSencondRecyclerAdapter);
        Bundle bundle = getArguments();
        int id = bundle.getInt(ID);

        String str = String.format(Locale.CHINA, "https://www.wanandroid.com/project/list/1/json?cid=%d",id);
        OkHttpClient okHttpClient1 = new OkHttpClient();
        Request builder1 = new Request.Builder()
                .url(str)
                .build();
        Call call1 = okHttpClient1.newCall(builder1);
        call1.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
            }
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                final String  string = response.body().string();
                Handler handler = new Handler(Looper.getMainLooper());
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        SencondRecyclerEntity sencondRecyclerEntity = new Gson().fromJson(string, SencondRecyclerEntity.class);
                        if (sencondRecyclerEntity.getErrorCode() ==0) {
                            SencondRecyclerEntity.DataBean data = sencondRecyclerEntity.getData();
                            if (isRefresh) {
                                fragmentSencondRecyclerAdapter.replaceData(data.getDatas());
                            }else {
                                if (data.getDatas().size()>0) {
                                    fragmentSencondRecyclerAdapter.addData(data.getDatas());
                                }else {
                                    Toast.makeText(getContext(),"666",Toast.LENGTH_SHORT).show();
                                }
                            }
                        }
                    }
                });
            }
        });
      /*  String format = String.format(Locale.CHINA, "https://www.wanandroid.com/project/list/1/json?cid=%d", id);
        OkHttpClient okHttpClient = new OkHttpClient();
        Request builder = new Request.Builder()
                .url(format)
                .build();
        Call call = okHttpClient.newCall(builder);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String string = response.body().string();
                SencondRecyclerEntity sencondRecyclerEntity = new Gson().fromJson(string, SencondRecyclerEntity.class);
                if (sencondRecyclerEntity.getErrorCode()==0) {
                    datas = sencondRecyclerEntity.getData().getDatas();
                   mEneityList.addAll(datas);

                }
            }
        });*/
    }
}
